﻿using System;
using System.Collections.Generic;
using KKCai.TV.Model;
using KKCai.TV.DAL;
using Newtonsoft.Json;

namespace KKCai.TV
{
    public class Wapper
    {
        public IList<RecordInfo> record;

        public IList<int> count;
    }

    public class Manager
    {
        public string List(int limit)
        {
            Wapper w = new Wapper { 
                record = new RecordDao().List(limit), 
                count = new DayCountDao().List() 
            };

            return JsonConvert.SerializeObject(w);
        }

        public void Save(string phase, string result)
        {
            RecordDao dao = new RecordDao();

            RecordInfo r = dao.Order("id", false, 1)[0];

            int last = Convert.ToInt32(r.Phase.Substring(8));
            int newid = Convert.ToInt32(phase.Substring(8));
            if ((newid == last + 1) || (newid == 1 && last == 84))
            {
                dao.Save(new RecordInfo(phase, result));

                DayCountDao ddao = new DayCountDao();
                if (newid == 1)
                    ddao.Reset();

                ddao.Update(result);           
            }
        }
    }
}
